# -*- coding: utf-8 -*-#
__author__ = 'yangq'

from tornado import gen
import copy
from functools import wraps
import models as paym
from couch import models as couchm
from common import utiltool


@gen.coroutine
def deal_wx_pay_success_order(wxdict):
    order_info = yield paym.get_order_with_out(wxdict['out_trade_no'])
    print order_info
    if order_info:
        if order_info['order_type'] == 'vip1':
            yield deal_wx_pay_vip1(order_info['phone'], order_info['from_phone'], wxdict['trade_type'])
        elif order_info['order_type'] == 'vip4':
            yield deal_wx_pay_vip4(order_info['phone'], order_info['from_phone'], wxdict['trade_type'])
        elif order_info['order_type'] == 'bmt':
            yield deal_wx_pay_bmt(order_info['phone'], wxdict['trade_type'])
        else:
            pass


@gen.coroutine
def deal_wx_pay_bmt(phone, trade_type):
    if trade_type == 'JSAPI':
        yield paym.open_bmt(phone)
        yield couchm.add_couch_score(
            phone,
            "open_bmt",
            'open_bmt',
            100
        )
    else:
        yield paym.open_bmt(phone)
        yield couchm.add_couch_score(
            phone,
            "open_bmt",
            'open_bmt',
            100
        )


@gen.coroutine
def deal_wx_pay_vip1(phone, from_phone, trade_type):
    if trade_type == 'JSAPI':
        yield paym.open_vip1(phone)
        # yield couchm.add_couch_score(
        #     phone,
        #     from_phone,
        #     'open_vip',
        #     10
        # )
    else:
        yield paym.add_score_open_vip(phone)
        yield paym.open_vip1(phone)


@gen.coroutine
def deal_wx_pay_vip4(phone, from_phone, trade_type):
    if trade_type == 'JSAPI':
        yield paym.open_vip4(phone)
        # yield couchm.add_couch_score(
        #     phone,
        #     from_phone,
        #     'open_vip',
        #     10
        # )
    else:
        yield paym.add_score_open_vip(phone)
        yield paym.open_vip4(phone)


@gen.coroutine
def deal_ali_pay_success_order(resdict):
    order_info = yield paym.get_order_with_out(resdict['out_trade_no'][0])
    if order_info:
        if order_info['order_type'] == 'vip1':
            yield deal_ali_pay_vip1(order_info['phone'], order_info['from_phone'])
        elif order_info['order_type'] == 'vip4':
            yield deal_ali_pay_vip4(order_info['phone'], order_info['from_phone'])
        elif order_info['order_type'] == 'bmt':
            yield deal_ali_pay_bmt(order_info['phone'])
        else:
            pass


@gen.coroutine
def deal_ali_pay_vip1(phone, from_phone):
    yield paym.add_score_open_vip(phone)
    yield paym.open_vip1(phone)


@gen.coroutine
def deal_ali_pay_vip4(phone, from_phone):
    yield paym.add_score_open_vip(phone)
    yield paym.open_vip4(phone)


@gen.coroutine
def deal_ali_pay_bmt(phone):
    yield paym.open_bmt(phone)
    yield couchm.add_couch_score(
        phone,
        "open_bmt",
        'open_bmt',
        100
    )


def check_id_code(func):
    """
    检测用户本次的access_token是否有效
    :param request_handler: request对象请求
    :return:{'code':'','msg':''}
    """
    @wraps(func)
    @gen.coroutine
    def check(*args, **kw):
        """
        """
        para_dict = args[0].request.arguments
        phone = para_dict['phone'][0]
        id_code = para_dict['id_code'][0]
        id_code_info = yield paym.get_last_id_code_with_phone(phone)
        if id_code_info and id_code_info['id_code'] == id_code:
            yield func(*args)
        else:
            utiltool.response_write(args[0], {"code": "1011", "msg": u"验证码错误"})
    return check
